Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: runtime primitives for mpz objects #6395

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

tydeu
Copy link
Member

@tydeu tydeu commented Dec 16, 2024

This PR adds a basic C API interface for Lean MPZ objects. It includes constructors, conversions, and comparison operators.

These primitives are necessary to correctly and efficiently model runtime MPZ objects in Lean.

Where applicable, existing APIs were adjusted to use the new interface.

@tydeu tydeu force-pushed the lean-mpz-primitives branch 3 times, most recently from 69ed417 to 4a08e12 Compare December 16, 2024 02:43
@github-actions github-actions bot added the toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN label Dec 16, 2024
leanprover-community-mathlib4-bot added a commit to leanprover-community/batteries that referenced this pull request Dec 16, 2024
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Dec 16, 2024
@leanprover-community-bot
Copy link
Collaborator

leanprover-community-bot commented Dec 16, 2024

Mathlib CI status (docs):

@leanprover-community-bot leanprover-community-bot added the builds-mathlib CI has verified that Mathlib builds against this PR label Dec 16, 2024
@tydeu tydeu marked this pull request as ready for review December 16, 2024 04:07
@tydeu tydeu added the changelog-library Library label Dec 16, 2024
@tydeu tydeu marked this pull request as draft December 16, 2024 04:13
@leodemoura
Copy link
Member

Recall that we should be able to build Lean with and without GMP: https://github.com/leanprover/lean4/blob/master/src/CMakeLists.txt#L77

@tydeu
Copy link
Member Author

tydeu commented Dec 16, 2024

@leodemoura Yep! The good news is that these primitives are abstracted above the MPZ implementation. (They work with either GMP or our homegrown multiple precision integer implementation.)

@tydeu tydeu force-pushed the lean-mpz-primitives branch from e453b34 to ea15ca7 Compare December 16, 2024 04:45
leanprover-community-mathlib4-bot added a commit to leanprover-community/batteries that referenced this pull request Dec 16, 2024
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Dec 16, 2024
@tydeu tydeu force-pushed the lean-mpz-primitives branch from ea15ca7 to dc5e674 Compare December 16, 2024 05:24
leanprover-community-mathlib4-bot added a commit to leanprover-community/batteries that referenced this pull request Dec 16, 2024
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Dec 16, 2024
static inline int64_t lean_scalar_to_int64(b_lean_obj_arg a) {
assert(lean_is_scalar(a));
if (sizeof(void*) == 8)
return (int)((unsigned)lean_unbox(a)); /* NOLINT */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this truncating to 32 bits?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eric-wieser Scalar integers in the Lean runtime are <= 32 bits. These definitions were just moved up.


extern "C" LEAN_EXPORT obj_res lean_mpz_of_int32(uint32_t n) {
static_assert(sizeof(uint32_t) <= sizeof(int), "int should be at least the size of a uint32");
return alloc_mpz(mpz(static_cast<int>(static_cast<int32_t>(n))));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the pair of casts vs letting a constructor overload handle it?

Copy link
Member Author

@tydeu tydeu Jan 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eric-wieser There is not int32_t/int32 constructor for mpz. I also wanted to avoid changes to the mpz implentation, focusing instead on just the public API.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, perhaps adding more constructor overloads to mpz would be clearer?

@kim-em kim-em added the awaiting-author Waiting for PR author to address issues label Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting-author Waiting for PR author to address issues builds-mathlib CI has verified that Mathlib builds against this PR changelog-library Library toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants